package com.migration.model.migration.scripts;

import com.migration.model.migration.AbstractMigrationScript;

/**
 * 风险设备偏离关联表(risk_device_deviation)迁移脚本
 */
public class A007_RiskDeviceDeviationMigrationScript extends AbstractMigrationScript {

    /**
     * 构造函数，初始化迁移配置
     */
    public A007_RiskDeviceDeviationMigrationScript() {
        super(
            "risk_device_deviation",
            "风险设备偏离关联数据迁移",
            "多表关联",
            "risk_device_deviation",
            7
        );
    }

    /**
     * 生成迁移数据的SQL
     *
     * @return 迁移数据的SQL
     */
    @Override
    protected String generateMigrationSql() {
        return "INSERT INTO risk_device_deviation (\n" +
               "  id,\n" +
               "  department_id,\n" +
               "  device_group_id,\n" +
               "  device_id,\n" +
               "  deviation_id,\n" +
               "  cause_quantity,\n" +
               "  consequence_quantity\n" +
               ")\n" +
               "SELECT\n" +
               "  r.id,\n" +
               "  n.tenant_id AS department_id,\n" +
               "  ng.group_id AS device_group_id,\n" +
               "  n.id AS device_id,\n" +
               "  r.linked_id AS deviation_id,\n" +
               "  0 AS cause_quantity,\n" +
               "  0 AS consequence_quantity\n" +
               "FROM risk_lunnan_test.ra_node n\n" +
               "JOIN risk_lunnan_test.ra_node_relation r ON r.node_id = n.id AND r.linked_type = 1\n" +
               "LEFT JOIN risk_lunnan_test.ra_node_group ng ON ng.node_id = n.id;";
    }

    /**
     * 获取源数据SQL
     *
     * @return 源数据查询SQL
     */
    public String getSourceSql() {
        return "SELECT\n" +
               "  r.id,\n" +
               "  n.tenant_id,\n" +
               "  ng.group_id,\n" +
               "  n.id AS device_id,\n" +
               "  r.linked_id\n" +
               "FROM risk_lunnan_test.ra_node n\n" +
               "JOIN risk_lunnan_test.ra_node_relation r ON r.node_id = n.id AND r.linked_type = 1\n" +
               "LEFT JOIN risk_lunnan_test.ra_node_group ng ON ng.node_id = n.id;";
    }

    /**
     * 获取目标数据SQL模板
     *
     * @return 目标数据SQL模板
     */
    public String getTargetSqlTemplate() {
        return "INSERT INTO risk_device_deviation (\n" +
               "  id,\n" +
               "  department_id,\n" +
               "  device_group_id,\n" +
               "  device_id,\n" +
               "  deviation_id,\n" +
               "  cause_quantity,\n" +
               "  consequence_quantity\n" +
               ")";
    }
}
